<?php
session_start();
include 'nav.inc.php';
include 'dbConn.php';
html_head('Listen-Zugehörigkeit bearbeiten');

echo("<link rel='stylesheet' type='text/css' href='styleVerteiler.css' media='all' />\n");
echo "<script type='text/javascript' src='verteiler.js'></script>";

menu();

$lid = -1;
$doubleEntry = 0;
echo("<div class='content'>\n");
if (!isset($_REQUEST['submitBTN']) && isset($_REQUEST['lid'])) {
	$lid = $_REQUEST['lid'];
	
	$result = $dbVerteiler->getListByLID($lid);
	$user = mysql_fetch_array($result);
	$title = $user['Titel'];
} 

if (isset($_REQUEST['listmembers']) && count($_REQUEST['listmembers']) > 0) 
{
	$lid = $_REQUEST['lid'];
	$newOldAddresses = $_REQUEST['listmembers'];
	foreach ($newOldAddresses as $aid) {
		$dbVerteiler->removeAddressFromRightAssociation($aid);
	}
}

if (isset($_REQUEST['otherAddresses']) && count($_REQUEST['otherAddresses']) > 0) 
{
	$lid = $_REQUEST['lid'];
	$newAddresses = $_REQUEST['otherAddresses'];
	foreach ($newAddresses as $aid) {
		$dbVerteiler->addAddressToRight($aid, $lid);
	}
}

echo "<form id='modList' name='modList' action='".$_SERVER['SCRIPT_NAME']."' method='post' >\n";
$result = $dbVerteiler->getLists();
echo "\t<fieldset>
  <legend>Wähle Liste</legend>
";
	echo "<div id='listselect'><select id='lists' name='lists' size=10 onchange='showListInfo();'>";
while ($row = mysql_fetch_array($result))
{
	echo "<option value='".$row['LID']."'>".$row['Titel']."</option>";
}
echo "</select></div>
  </fieldset>";
echo "\t<fieldset>
 <legend id='listInfoLegend' style='display:none;'>".$title."</legend>
  <ol id='listInfo' style='display:none;' class='clearfix'>
    <li>
      <label for='listmembers[]'>Anschreibberechtigte der Liste:</label>";
      	echo "				<select name='listmembers[]' size='8' style='width:25em;'>";
		$result2 = $dbVerteiler->getWriteRightMembersOfListByLID($lid); 
		$members = array();
		while($row2 = mysql_fetch_array($result2))
		{
			  echo "			<option value='".$row2["AID"]."'>".$row2['Adresse']." (".$row2['Vorname']." ".$row2['Nachname'].")</option>";
			  $members[] = $row2['Adresse'];
		}
		$result2 = $dbVerteiler->getWriteRightListsOfListByLID($lid); 
		while($row2 = mysql_fetch_array($result2))
		{
			  echo "			<option value='".$row2["AID"]."'>".$row2['Adresse']." (Liste: ".$row2['Titel'].")</option>";
		}
		echo "				</select>";
echo "<input type='hidden' name='lid' id='lid' value='".$lid."' />
      <input type='hidden' name='index' id='index' value='-1' />
    </li>
    <li class='last'>
      		<input type='submit' name='submitBTN' id='submitBTN' value='Markierte entfernen' />
    </li>
<li >
	<label for='otherAddresses[]'>Neue Adresse eintragen:</label>
		<select multiple name='otherAddresses[]' size='8' style='width:25em;'>";
		$result2 = $dbVerteiler->getAllAdressesAndNames(); 
		while($row2 = mysql_fetch_array($result2))
		{
			if (!in_array($row2['Adresse'],$members))
				echo "<option value='".$row2['AID']."'>".$row2['Adresse']." (".$row2['Vorname']." ".$row2['Nachname'].")</option>";
		}
		echo "				</select>
</li>
<li class='last'>
	<input type='submit' name='submitBTN' id='submitBTN' value='Markierte hinzufügen' />
</li>
  </ol>
  </fieldset>";
	echo "\t</form>\n";

if ($doubleEntry)
{
	echo "<script language=javascript>
				changeModList(".$lid.");
		</script>";
}
	
if ($lid > -1)
		echo "<script type='text/javascript'>showNowForRealList('".$_REQUEST["index"]."');</script>";
echo "</div>";

?>
